package users

import (
	"gitee.com/xgotool/backend-framework-template/config"
	"gitee.com/xgotool/backend-framework-template/model"
)

// 获取菜单列表信息
func MenuList(pid uint) ([]model.Menu, error) {
	menus := make([]model.Menu, 0)
	// 获取该目录下的子菜单信息
	conn, err := config.GetConnect()
	if err != nil {
		return nil, err
	}
	mod := model.Menu{}
	err = conn.Table(mod.TableName()).Where("parent_id", pid).Order("order_num ASC").Find(&menus).Error
	if err != nil {
		return nil, err
	}
	// 递归获取子菜单信息
	for i := 0; i < len(menus); i++ {
		menus[i].Meta.Title = menus[i].Title
		menus[i].Meta.Icon = menus[i].Icon
		tmp, err := MenuList(menus[i].Id)
		if err == nil {
			menus[i].Childs = tmp
		}
	}
	return menus, nil
}
